package com.zhentao.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.payment.entity.PaymentOrder;
import com.zhentao.payment.mapper.PaymentOrderMapper;
import com.zhentao.payment.service.PaymentOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class PaymentOrderServiceImpl extends ServiceImpl<PaymentOrderMapper, PaymentOrder> implements PaymentOrderService {

    @Override
    @Transactional
    public PaymentOrder createOrder(PaymentOrder order) {
        // 生成订单号
        order.setOrderNo(generateOrderNo());
        // 设置初始状态
        order.setStatus(0);
        // 设置创建时间
        order.setCreateTime(LocalDateTime.now());
        // 设置更新时间
        order.setUpdateTime(LocalDateTime.now());
        
        // 保存订单
        save(order);
        return order;
    }

    @Override
    public PaymentOrder queryOrder(String orderNo) {
        LambdaQueryWrapper<PaymentOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentOrder::getOrderNo, orderNo);
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public void cancelOrder(String orderNo) {
        PaymentOrder order = queryOrder(orderNo);
        if (order != null && order.getStatus() == 0) {
            order.setStatus(2);
            order.setUpdateTime(LocalDateTime.now());
            updateById(order);
        }
    }

    @Override
    public IPage<PaymentOrder> queryOrderList(String orderNo, Integer status, Integer paymentType, Integer page, Integer size) {
        Page<PaymentOrder> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<PaymentOrder> wrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (StringUtils.hasText(orderNo)) {
            wrapper.like(PaymentOrder::getOrderNo, orderNo);
        }
        if (status != null) {
            wrapper.eq(PaymentOrder::getStatus, status);
        }
        if (paymentType != null) {
            wrapper.eq(PaymentOrder::getPaymentType, paymentType);
        }
        
        // 按创建时间降序排序
        wrapper.orderByDesc(PaymentOrder::getCreateTime);
        
        return page(pageParam, wrapper);
    }

    /**
     * 更新订单支付状态
     */
    @Transactional
    public void updatePaymentStatus(String orderNo, Integer status) {
        PaymentOrder order = queryOrder(orderNo);
        if (order != null) {
            order.setStatus(status);
            order.setUpdateTime(LocalDateTime.now());
            if (status == 1) { // 支付成功
                order.setPayTime(LocalDateTime.now());
            }
            updateById(order);
        }
    }

    @Override
    @Transactional
    public void updateOrderToPaid(String orderNo) {
        PaymentOrder order = queryOrder(orderNo);
        if (order != null && order.getStatus() == 0) {
            order.setStatus(1); // 设置为已支付状态
            order.setUpdateTime(LocalDateTime.now());
            updateById(order);
        }
    }

    /**
     * 生成订单号
     */
    private String generateOrderNo() {
        return "PAY" + System.currentTimeMillis() + UUID.randomUUID().toString().substring(0, 4);
    }
} 